package com.example.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.entity.Menu;
import com.example.auth.entity.MenuPermission;
import com.example.auth.entity.MetaProps;
import com.example.auth.entity.Permission;
import com.example.auth.mapper.MenuPermissionMapper;
import com.example.auth.mapper.PermissionMapper;
import com.example.auth.response.Result;
import com.example.auth.service.MenuService;
import com.example.auth.mapper.MenuMapper;
import com.example.auth.service.UserPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 13057
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2024-07-07 18:51:46
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuPermissionMapper menuPermissionMapper;

    @Autowired
    private UserPermissionService userPermissionService;

    @Override
    public Result getMenusByScopes(List<String> scopes) {
        JwtAuthenticationToken authentication =
                (JwtAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Jwt jwt = (Jwt) authentication.getPrincipal();
        String username = jwt.getClaimAsString("sub");
        // 获取对应scopes的权限ID列表
        List<Long> permissionIds = permissionMapper.selectList(null).stream()
                .filter(permission -> scopes.contains(permission.getName()))
                .map(Permission::getId)
                .collect(Collectors.toList());

        if (permissionIds.isEmpty()) {
            return Result.OK(Collections.emptyList());
        }

        // 获取权限对应的菜单ID列表
        List<Long> menuIds = menuPermissionMapper.selectList(null).stream()
                .filter(menuPermission -> permissionIds.contains(menuPermission.getPermissionId()))
                .map(MenuPermission::getMenuId)
                .distinct()
                .collect(Collectors.toList());

        if (menuIds.isEmpty()) {
            return Result.OK(Collections.emptyList());
        }

        // 获取菜单列表
        //List<Menu> menus = this.listByIds(menuIds);
        List<Menu> menus = this.list();
        // 构建菜单树
        List<Menu> list = buildMenuTree(menus, 0L);

        return Result.OK(list);
    }


    private List<Menu> buildMenuTree(List<Menu> allMenus, Long parentId) {
        List<Menu> menus = new ArrayList<>();
        for (Menu menu : allMenus) {
            if (menu.getParentId()==parentId) {
                menu.setChildren(buildMenuTree(allMenus, menu.getId()));
                menus.add(supplementMetaInfo(menu));
            }
        }
        return menus;
    }


    public Menu supplementMetaInfo(Menu menu){
        MetaProps props = MetaProps.builder()
                .icon(menu.getMetaIcon())
                .title(menu.getMetaTitle())
                .active(menu.getMetaActive())
                .isLink(menu.getMetaIsLink())
                .isHide(menu.getMetaIsHide())
                .isFull(menu.getMetaIsFull())
                .isAffix(menu.getMetaIsAffix())
                .isKeepAlive(menu.getMetaIsKeepAlive())
                .tag(menu.getMetaTag())
                .build();
        menu.setMeta(props);
        return menu;
    }


}




